Repository navigation
Json cleanup - #55
Json cleanup#55mmathieum wants to merge 4 commits into
Conversation
mmathieum
commented
Nov 23, 2026
- Json cleanup commons#935
There was a problem hiding this comment.
Code Review
This pull request introduces a new utility file JsonExt.kt containing extension functions to convert Kotlin objects to and from kotlinx.serialization.json elements. Feedback includes addressing a runtime exception when converting primitive arrays (which do not inherit from Array<*>) to JsonElement, and optimizing the toAny() function to avoid redundant parsing of primitive types.
| is Iterable<*> -> JsonArray(this.map { it.toJsonElement() }) | ||
| is Array<*> -> JsonArray(this.map { it.toJsonElement() }) | ||
| else -> throw IllegalArgumentException("Unsupported type for JsonElement: ${this::class}") |
There was a problem hiding this comment.
In Kotlin, primitive arrays (such as IntArray, LongArray, DoubleArray, etc.) do not inherit from Array<*>. As a result, passing any primitive array to toJsonElement() will fall through to the else branch and throw an IllegalArgumentException at runtime.
To make this function robust and support all array types, we should explicitly handle primitive arrays in the when expression.
| is Iterable<*> -> JsonArray(this.map { it.toJsonElement() }) | |
| is Array<*> -> JsonArray(this.map { it.toJsonElement() }) | |
| else -> throw IllegalArgumentException("Unsupported type for JsonElement: ${this::class}") | |
| is Iterable<*> -> JsonArray(this.map { it.toJsonElement() }) | |
| is Array<*> -> JsonArray(this.map { it.toJsonElement() }) | |
| is ByteArray -> JsonArray(this.map { it.toJsonElement() }) | |
| is ShortArray -> JsonArray(this.map { it.toJsonElement() }) | |
| is IntArray -> JsonArray(this.map { it.toJsonElement() }) | |
| is LongArray -> JsonArray(this.map { it.toJsonElement() }) | |
| is FloatArray -> JsonArray(this.map { it.toJsonElement() }) | |
| is DoubleArray -> JsonArray(this.map { it.toJsonElement() }) | |
| is BooleanArray -> JsonArray(this.map { it.toJsonElement() }) | |
| is CharArray -> JsonArray(this.map { it.toJsonElement() }) | |
| else -> throw IllegalArgumentException("Unsupported type for JsonElement: ${this::class}") |
| is JsonPrimitive -> when { | ||
| isString -> content | ||
| booleanOrNull != null -> boolean | ||
| longOrNull != null -> long | ||
| doubleOrNull != null -> double | ||
| else -> content | ||
| } |
There was a problem hiding this comment.
In the current implementation, standard library functions like booleanOrNull, longOrNull, and doubleOrNull are called to check if the value matches a type, and then the corresponding throwing properties (boolean, long, double) are called again to retrieve the value. This results in parsing the string representation twice for each type check.
We can optimize this by using the Elvis operator (?:) to retrieve and return the parsed value directly, avoiding redundant parsing.
is JsonPrimitive -> if (isString) {
content
} else {
booleanOrNull ?: longOrNull ?: doubleOrNull ?: content
}|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|